package com.example.coding_problem.Fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.coding_problem.R;
import com.example.coding_problem.Vehice_violation_stl;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

import static android.support.constraint.Constraints.TAG;

public class Vehicle_violation extends Fragment {
    private EditText input_box;
    private Button query;
    private List<Vehice_violation_stl>vvstl=new ArrayList<>();
    public Vehicle_violation() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.vehicle_violation, container, false);
        initView(view);
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (input_box.length()==0){
                    Toast.makeText(getContext(), "请输入车牌号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!input_box.getText().toString().trim().equals("A10001")&&!input_box.getText().toString().trim().equals("A10002")&&!input_box.getText().toString().trim().equals("A10003")&&!input_box.getText().toString().trim().equals("A10004")&&!input_box.getText().toString().trim().equals("A10005")&&!input_box.getText().toString().trim().equals("A10006")){
                    Toast.makeText(getContext(), "请输入正确的车牌号", Toast.LENGTH_SHORT).show();
                    return;
                }
                    initData();
            }
        });
        return view;
    }
    public interface BlogService1{
        @POST("getWeiZhangLog")
        Call<ResponseBody> getBlog1(@Body RequestBody requestBody1);
    }
    private void showSetDeBug(){
        AlertDialog.Builder setDeBugDialog = new AlertDialog.Builder(getContext());
        //获取界面
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.vihicle_violation_list, null);
        //将界面填充到AlertDiaLog容器
        setDeBugDialog.setView(dialogView);
        //初始化控件
        ListView lvq = dialogView.findViewById(R.id.lvq) ;
        lvq.setAdapter(new VVAdapter());
        Button tuichu=dialogView.findViewById(R.id.bu_tuichu);
        //取消点击外部消失弹窗
        setDeBugDialog.setCancelable(false);
        //创建AlertDiaLog
        setDeBugDialog.create();
        //AlertDiaLog显示
        final AlertDialog customAlert = setDeBugDialog.show();
        //退出功能
        tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customAlert.dismiss();
            }
        });
        //逻辑事件
    }
    private void initView(View view) {
        query=view.findViewById(R.id.query);
        input_box=view.findViewById(R.id.input_box);
    }
  //  String str[] ={"鲁A10001","鲁A10002","鲁A10003","鲁A10004","鲁A10005","鲁A10006"};
    private void initData(){
        Retrofit retrofit1=new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        Map<String,String> map = new HashMap<>();
        map.put("CarNo","鲁"+input_box.getText().toString().trim());
        final Gson gson = new Gson();
        Log.v("Tag","WWE"+gson.toJson(map));
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),gson.toJson(map));
        BlogService1 service1=retrofit1.create(BlogService1.class);
        Call<ResponseBody>call2=service1.getBlog1(requestBody1);
        call2.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Vehice_violation_stl carlist= null;
                try {
                    carlist = gson.fromJson(response.body().string(), Vehice_violation_stl.class);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                vvstl.add(carlist);
                showSetDeBug();
        }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
    class VVAdapter extends BaseAdapter {

        @Override
        public int getCount() {

            Log.v("Tag","erre"+vvstl.get(0).getData().size());
            return vvstl.get(0).getData().get(0).getLog().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(getContext(),R.layout.vihicle_violation1,null);
            TextView address = (TextView) view1.findViewById(R.id.address);
            TextView fee = (TextView) view1.findViewById(R.id.fee);
            TextView reason = (TextView) view1.findViewById(R.id.reason);
            address.setText(vvstl.get(0).getData().get(0).getLog().get(i).getAddress()+"");
            fee.setText(vvstl.get(0).getData().get(0).getLog().get(i).getFee()+"");
            reason.setText(vvstl.get(0).getData().get(0).getLog().get(i).getReason()+"");
            return view1;
        }
    }
}
